package com.nttuan.mycalender;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.support.v4.app.Fragment;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.nttuan.adapter.FreeTimeAdapter;
import com.nttuan.flows.FreeTimeListener;
import com.nttuan.flows.PageChange;
import com.nttuan.model.dao.TaskDao;
import com.nttuan.model.dao.service.TaskService;
import com.nttuan.model.entity.FreeTime;
import com.nttuan.model.entity.Task;
import com.nttuan.util.Support;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class FragmentFreeTime extends Fragment implements View.OnClickListener, PageChange, FreeTimeListener,AdapterView.OnItemClickListener{

    public static final String TAG = "fragmentFreeTime";
    public static final String KEY_DATE_START = "startDate";
    public static final String KEY_DURING = "during";
    public static final String KEY_DATE_END= "endDate";

    private Context context;
    private ListView listView;
    private ProgressBar processBar;

    private FreeTimeAdapter freeTimeAdapter;
    private List<FreeTime> listFreeTime;

    private TaskService taskService;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_free_time, container, false);
        init(rootView);

        //code here

		return rootView;
	}
    private void init(View rootView){
        //init view
        listView =(ListView)rootView.findViewById(R.id.listViewFreeTime);
        processBar =(ProgressBar)rootView.findViewById(R.id.progressBarFreeTime);
        //init
        taskService = TaskDao.getInstance(context);
        listFreeTime = new ArrayList<FreeTime>();
        freeTimeAdapter = new FreeTimeAdapter(getActivity(), listFreeTime);
        listView.setAdapter(freeTimeAdapter);
        //init values

        //init action

        freeTimeAdapter.setListener(this);
        listView.setOnItemClickListener(this);
    }

    private class loadFreeTime extends AsyncTask<String,Void,Void>{
         private ProgressDialog dialog;
        private Date date;
        public loadFreeTime(Date date){
            this.date = date;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            processBar.setVisibility(View.VISIBLE);
            getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
        }
        @Override
        protected Void doInBackground(String... sDate) {
         //   Date onDate = Support.toDateTime(textViewByDate.getText().toString(),true);
            listFreeTime = taskService.freeTime(context, date, true);
            freeTimeAdapter = new FreeTimeAdapter(context, listFreeTime);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            processBar.setVisibility(View.GONE);
            getActivity().getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
//            dialog.dismiss();
            updateAdapter();

        }
    }
    private void updateAdapter(){
        freeTimeAdapter.setListener(this);
        listView.setAdapter(freeTimeAdapter);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){

        }
    }
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = getActivity();
        ((MainActivity)context).pageChangeFreeTime = this;
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        View childView;
        for(int count = 0 ; count< adapterView.getChildCount(); count++) {
            childView = adapterView.getChildAt(count);
//            View previousTime = childView.findViewById(R.id.lLayoutTimePrevious);
            View previousLayout = childView.findViewById(R.id.rLayoutPreviousTask);
            View nextLayout = childView.findViewById(R.id.rLayoutNextTask);
            if (i == count)
                continue;
            previousLayout.setVisibility(View.GONE);
            nextLayout.setVisibility(View.GONE);
        }

        View previousLayout = view.findViewById(R.id.rLayoutPreviousTask);
        View nextLayout = view.findViewById(R.id.rLayoutNextTask);
        if(previousLayout.getVisibility() == View.VISIBLE) {
            previousLayout.setVisibility(View.GONE);
        }
        else {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.show_previous);
            previousLayout.startAnimation(animation);
            previousLayout.setVisibility(View.VISIBLE);
        }
        if(nextLayout.getVisibility() == View.VISIBLE) {
            nextLayout.setVisibility(View.GONE);
        }
        else {
            Animation animation = AnimationUtils.loadAnimation(context, R.anim.show_next);
            nextLayout.startAnimation(animation);
            nextLayout.setVisibility(View.VISIBLE);
        }
    }
    @Override
    public void selectedPage(int page, Date date) {
        if (page == 1) {
            Log.d(TAG, Support.toString(date));
            new loadFreeTime(date).execute();
        }
    }

    @Override
    public void changeDate(Date date) {
        new loadFreeTime(date).execute();
    }

    @Override
    public void viewBy(String by) {

    }

    @Override
    public void addNew(FreeTime freeTime) {
        Bundle bundle = new Bundle();
        bundle.putInt(AddNewActivity.KEY_ACTION,AddNewActivity.KEY_ACTION_ADD_FREE);
        bundle.putString(Task.DATE_START, Support.toString(freeTime.getFrom()));
        bundle.putInt(Task.DURING, freeTime.getDuring());
      //  bundle.putString(Task.DATE_END, Support.toString(Support.addDateTime(freeTime.getFrom(),freeTime.getDuring())));
        Intent intent = new Intent(context, AddNewActivity.class);
        intent.putExtra(AddNewActivity.KEY_BUNDLE, bundle);
        startActivity(intent);
    }

}
